package com.a214.yxz.yuejian013.Activity;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Base.BaseSActivity;
import com.a214.yxz.yuejian013.Fragment.ClubInfoFragment;
import com.a214.yxz.yuejian013.Fragment.ClubVideoFragment;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;
import com.a214.yxz.yuejian013.Fragment.ClubCourseFragment;
import com.squareup.picasso.Picasso;

public class ClubDetailActivity extends BaseSActivity {

    private ImageButton return_btn,fav_btn;
    private ImageView club_pic;
    private RadioGroup clubDetail;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private ClubInfoFragment infoFragment;
    private ClubCourseFragment courseFragment;
    private ClubVideoFragment videoFragment;
    private FrameLayout frameLayout;
    private ResModel model=new ResModel("",getSessionID());
    private ServiceListener<String> col_Re_listener,col_listener,uncol_listener;
    private String club_id,thumb;
    private int state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);

        return_btn= (ImageButton) findViewById(R.id.club_detail_return);
        fav_btn= (ImageButton) findViewById(R.id.club_detail_fav);
        club_pic= (ImageView) findViewById(R.id.club_detail_clubPic);
        clubDetail= (RadioGroup) findViewById(R.id.clubDetail_RadioGroup);
        frameLayout= (FrameLayout) findViewById(R.id.club_detail_frameLayout);
        fragmentManager=getSupportFragmentManager();
        club_id=getIntent().getStringExtra("club_id");
        thumb=getIntent().getStringExtra("pic");

        Picasso.with(ClubDetailActivity.this).load("http://172.24.10.175/workout/Uploads/"+thumb).into(club_pic);

        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        clubDetail.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.clubDetail_R_info:
                        infoFragment=new ClubInfoFragment();
                        replaceFrag(infoFragment);
                        break;
                    case R.id.clubDetail_R_course:
                        courseFragment=new ClubCourseFragment();
                        replaceFrag(courseFragment);
                        break;
                    case R.id.clubDetail_R_video:
                        videoFragment=new ClubVideoFragment();
                        replaceFrag(videoFragment);
                        break;
                }
            }
        });
        RadioButton r_btn= (RadioButton) findViewById(R.id.clubDetail_R_info);
        r_btn.setChecked(true);


        col_Re_listener=new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                state=Integer.valueOf(l);
                switch (state){
                    case 0:
                        fav_btn.setBackgroundResource(R.drawable.fav_selected);
                        break;
                    case 1:
                        fav_btn.setBackgroundResource(R.drawable.fav);
                        break;
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ClubDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
        col_listener=new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                Toast.makeText(ClubDetailActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                getCollectState();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ClubDetailActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        };
        uncol_listener=new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                Toast.makeText(ClubDetailActivity.this, "取消关注", Toast.LENGTH_SHORT).show();
                getCollectState();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ClubDetailActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        };

        getCollectState();
        fav_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (state){
                    case 0:
                        model.uncollect(club_id,getUserId(),"1",uncol_listener);
                        break;
                    case 1:
                        model.collect(club_id,getUserId(),"1",col_listener);
                        break;
                }
            }
        });

    }


    private void replaceFrag(Fragment fragment) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.club_detail_frameLayout, fragment);
        fragmentTransaction.commit();
    }

    private void getCollectState(){
        model.getCollectionResult(club_id,getUserId(),"1",col_Re_listener);
    }
}
